package cn.edu.nju.askinginhand.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import cn.edu.nju.askinginhand.util.JSONParser;
import cn.edu.nju.askinginhand.util.LocationGetter;
import cn.edu.nju.askinginhand.util.NetworkTest;

/*import com.renren.api.connect.android.AsyncRenren;
import com.renren.api.connect.android.Renren;
import com.renren.api.connect.android.view.ConnectButton;
*/
/**
 * 
 * @author ZhangZu 登录界面ͨ
 */
public class LoginActivity extends Activity {
	// 人人key
	private static final String APIKEY = "a70713ea8bec406caa005372ada95498";

	// private Spinner loginTypeSpinner;
	private Button loginBtn;
	private Button registerBtn;
	private EditText passwordEdit;
	private EditText usernameEdit;
	private CheckBox rememberNameBox;
    public static  String uid="";
    public static  String personalpassword="";
    public static  String filter=".action";
/*	private ConnectButton renrenlogin;
	private RenrenLoginListener renrenloginlistener;
*/
/*	private String renrenloginID;

	public String getRenrenloginID() {
		return renrenloginID;
	}

	public void setRenrenloginID(String renrenloginID) {
		this.renrenloginID = renrenloginID;
	}*/

/*	private Renren renren;
	public AsyncRenren asyncRenren;

	public AsyncRenren getAsyncRenren() {
		return asyncRenren;
	}*/

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		// loginTypeSpinner = (Spinner) findViewById(R.id.loginTypeSpinner);
		loginBtn = (Button) findViewById(R.id.loginBtn);
		registerBtn = (Button) findViewById(R.id.registerBtn);
		passwordEdit = (EditText) findViewById(R.id.passwordEdit);
		usernameEdit = (EditText) findViewById(R.id.usernameEdit);
		rememberNameBox = (CheckBox) findViewById(R.id.rememberNameBox);
/*		renrenlogin = (ConnectButton) findViewById(R.id.login);
*/		// initSpinner();
		initLoginBtn();
		initRegisterBtn();
/*		this.setRenren(new Renren(APIKEY));
		this.getRenren().restorSessionKey(this);
		this.asyncRenren = new AsyncRenren(getRenren());
		this.renrenlogin.init(getRenren(), this);
		renrenloginlistener = new RenrenLoginListener(this);
		this.renrenlogin.setConnectButtonListener(renrenloginlistener);
*/
		httpTest();
	}

	/*
	 * public void initSpinner() {
	 * 
	 * List<String> list = new ArrayList<String>();
	 * list.add("AskingInHand账号登录"); list.add("人人账号登录"); ArrayAdapter adapter =
	 * new ArrayAdapter(this, R.layout.login_spinner_item, R.id.spinnerText,
	 * list); loginTypeSpinner.setAdapter(adapter); }
	 */

	public void initLoginBtn() {

		loginBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String password = passwordEdit.getText().toString();
				String username = usernameEdit.getText().toString();
				if (rememberNameBox.isSelected()) {
					/* TODO : remember name */
				}
				/*
				 * switch (loginTypeSpinner.getSelectedItemPosition()) { case 0:
				 * System.out.println("Login by asking in hand"); break; case 1:
				 * System.out.println("Login by renren"); break; default:
				 * System.out.println("Error"); }
				 */
				/**login process*/
				String msg = getLoginMsg(0,username,password);
				if(msg==null) {
					Toast.makeText(LoginActivity.this,"服务器错误",Toast.LENGTH_LONG).show();
					return;
				}

				if(msg.equals("fail")) {
					Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_LONG).show();
					return;
				}
				if(msg.equals("notexist")) {
					Toast.makeText(LoginActivity.this,"不存在的用户",Toast.LENGTH_LONG).show();
					return;
				}
				
				uid=msg;
				personalpassword=password;
				System.out.println(msg);
				startActivity(new Intent(LoginActivity.this, MainTab.class));
				LocationGetter.getInstance().init(
						LoginActivity.this.getApplicationContext());
			}
		});
	}
	
	public String getLoginMsg(int type,String username,String password) {
		List <NameValuePair> params = new ArrayList <NameValuePair>();
		params.add(new BasicNameValuePair("type", type+""));
		params.add(new BasicNameValuePair("userName", username));
		params.add(new BasicNameValuePair("password", password)); 
		String raw = JSONParser.getOut("UserLoginAction"+filter,params);
		//Toast.makeText(this, raw, Toast.LENGTH_SHORT).show();		
		return JSONParser.getStringData(raw);
	}

	public void initRegisterBtn() {
		registerBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				startActivity(new Intent(LoginActivity.this,
						RegisterActivity.class));
			}
		});
	}

	/**
	 */
	public void httpTest() {
		if (!NetworkTest.isNetworkAvailable(this)) {
			Toast.makeText(this, "请检查您的网络连接状态", Toast.LENGTH_SHORT).show();
		} else {
		}
	}

	/**
	 * 此activity结束时退出程序
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.exit(0);
		//android.os.Process.killProcess(android.os.Process.myPid());
	}
	
/*	public void setRenren(Renren renren) {
		this.renren = renren;
	}

	public Renren getRenren() {
		return renren;
	}*/
}